package com.common.test;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created by Sylar on 2016/6/24.
 * 供应链算法，计算利息，服务费，风险基金等计算
 */
public class LibraLoanCalculate {
    public static void main(String[] args) {
        //准备数据
        BigDecimal planrate=new BigDecimal("0.05");//理财年化利率
        BigDecimal riskrate=new BigDecimal("0.03");//风险金费率
        BigDecimal loanamount=new BigDecimal("66.29");//债权金额
        BigDecimal period=new BigDecimal("1");//债权期限
        BigDecimal monthrate=new BigDecimal("0.0278");//综合月费率



        BigDecimal intere=interest(planrate,riskrate,loanamount,period);//总利息
        BigDecimal intere_month=interest_month(planrate,riskrate,loanamount,period);//月利息
        BigDecimal fee=fee(monthrate,loanamount,period).subtract(intere);//服务费总额
        BigDecimal intere_month2=interest_month2(planrate,riskrate,loanamount,period);//月利息（不包含风险金）
        BigDecimal risk_month=loanamount.multiply(riskrate).divide(period,10, RoundingMode.DOWN);//月风险金

        System.out.println("总利息："+intere);
        System.out.println("服务费总额："+fee);//总费用-利息总额
        System.out.println("月利息："+intere_month);
        System.out.println("月利息（不包含风险金）："+intere_month2);
        System.out.println("月风险金："+risk_month);

        //System.out.println(interest2(new BigDecimal("0.06"),new BigDecimal("0.0314"),new BigDecimal("13434.24"),new BigDecimal("3")));
        //System.out.println(interest2(0.06,0.0314, (long) 13434.24,3));
    }

    //月息（包含风险金）
    public static BigDecimal interest_month(BigDecimal planrate, BigDecimal riskrate, BigDecimal loanamount, BigDecimal period){

        return  loanamount.multiply(planrate.divide(new BigDecimal("12"),10, RoundingMode.DOWN).add((riskrate.divide(period,10, RoundingMode.DOWN))));
    }

    //利息总额
    public static BigDecimal interest(BigDecimal planrate, BigDecimal riskrate, BigDecimal loanamount, BigDecimal period){

        return  loanamount.multiply(planrate).multiply(period).divide(new BigDecimal("12"),10, RoundingMode.DOWN).add(loanamount.multiply(riskrate));
    }
    //总费用
    public static BigDecimal fee(BigDecimal monthrate, BigDecimal loanamount, BigDecimal period){
        return monthrate.multiply(period).multiply(loanamount);
    }

    //月息（不包含风险金），用于利息期缴计算
    public static BigDecimal interest_month2(BigDecimal planrate, BigDecimal riskrate, BigDecimal loanamount, BigDecimal period){

        return  loanamount.multiply(planrate.divide(new BigDecimal("12"),10, RoundingMode.DOWN));
    }
}
